import {
  Format
} from '../../utils/common'
import {
  Common
}
from "../../utils/common";
import {
  MyModel
} from '../../models/my'
const myModel = new MyModel()
const app = getApp();
const format = new Format()
const common = new Common()
Page({
    data: {
        openIfmemberpop: false, // 是否显示选择会员状态弹窗
        showConsume: false, // 是否显示选择会员状态弹窗-是否显示一键加油或者直接战舰
        consumeTxt: '', // 会员状态弹窗文字
        modalOpened4: '', // 首页运营活动弹窗是否显示
        MainActive: '', // 运营活动弹窗链接及内容
        firstClick: true,
    },
    onLoad(options) {
      // console.log('homeonLoad' + JSON.stringify(options))
      this.getMainActiveParam()
    },
    onShow: function (options) {
      this.setData({
        firstClick: true,
        openIfmemberpop: false
      })
      my.removeStorageSync({
        key: 'activeTab',
      });
      my.removeStorageSync({
        key: 'time',
      });
      // console.log('homeonShow' + JSON.stringify(options))
      app.myLogin().then(r => {
        my.hideLoading();
        // console.log(app.globalData.user)
      })
    },
    getMainActiveParam() {
      myModel.getMainActiveParam(1).then(res => {
        // console.log(res)
        if (res) {
          if (res.imageSrc) {
            if (res.linkSrc) {
              this.setData({
                modalOpened4: true,
                "MainActive[linkSrc]": res.linkSrc,
                "MainActive[imageSrc]": res.imageSrc,
                "MainActive[height]": res.height
              })
            } else {
              this.setData({
                "MainActive[imageSrc]": res.imageSrc,
                "MainActive[height]": res.height,
                modalOpened4: true
              })
            }
          }
        }
        
      })
    },
    // 点击底部按钮
    toPage(e) {
      if (this.data.firstClick) {
        this.setData({
          firstClick: false,
        })
      } else {
        return
      }
      const url = e.currentTarget.dataset.url
      // this.setData({
      //   consumeTxt: this.data.consumeTxt,
      //   showConsume: this.data.showConsume,
      //   openIfmemberpop: true,
      // })
      // return
      if (app.globalData.user) {
        
        if (app.globalData.user.cardAsn) {
          if (url == 'mainAactive' && this.data.MainActive.linkSrc) {
            this.goUrl(this.data.MainActive.linkSrc)
            return
          }
          this.goUrl(url)
        } else {
          var consumeTxt,showConsume
          if (url == '/pages/oil/oilIndex/oilIndex') {
            showConsume = true
            consumeTxt = '直接消费'
          } else if (url == '/pages/order/myOrder/myOrder') {
            showConsume = true
            consumeTxt = '直接查看'
          } else {
            showConsume = false
          }
          this.setData({
            consumeTxt: consumeTxt,
            showConsume: showConsume,
            openIfmemberpop: true,
            modalOpened4: false
          })
        }
      } else {
        this.data.firstClick = true
        my.showToast({
          type: 'none',
          content: '获取信息失败,请重试',
          duration: 1000,
        });
      }
      
    },
    // 关掉弹窗
    onClose() {
      this.setData({
        firstClick: true,
        openIfmemberpop: false,
      });
    },
    // 关掉运营活动弹窗
    onModalClick4() {
      this.setData({
        firstClick: true,
        modalOpened4: false,
      });
    },
    // 前往页面
    goUrl(url) {
      let vm = this
      if (url == 'car' ) {
        my.alert({
          // title: '很抱歉',
          content: '功能上线中，敬请期待~',
          buttonText: '我知道了',
          success: () => {
            vm.data.firstClick = true
          }
        });
        return
      }
      this.setData({
        modalOpened4: false
      })
      my.navigateTo({
        url: "/" +url,
      })
    },
});